package com.aaa.power.util;


import com.aaa.common.constant.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @fileName.GlobalExceptionHandler
 * @description:
 * @author:zwl
 * @createTime:2020/11/9 16:13
 * @version:1.0.0
 */
//controller 的所有请求都会被拦截走这里
@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 拦截所有异常，做统一处理
     * @param ex
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    public Result exceptionHandler(Exception ex){
        ex.printStackTrace();
//        判断异常是否为自定义异常
        if(ex instanceof CustomException){
            //返回自定义异常
            return new Result(((CustomException) ex).getErrorCode(),((CustomException) ex).getErrorMsg(),"自定义异常");
        }
        //系统异常
        return new Result(ReturnCode.FAIL.getCode(),ReturnCode.FAIL.getMsg(),ex.getClass().getName());
    }
}
